import appconst from "./appconst.js";
export default {
	readOnlyChange() {
		this.readOnly = !this.readOnly
	},
	undo() {
		this.editorCtx.undo()
	},
	redo() {
		this.editorCtx.redo()
	},
	format(e) {
		let {
			name,
			value
		} = e.target.dataset
		if (!name) return
		// console.log('format', name, value)
		this.editorCtx.format(name, value)
	
	},
	onStatusChange(e) {
		const formats = e.detail
		this.formats = formats
	},
	insertDivider() {
		this.editorCtx.insertDivider({
			success: function() {
				console.log('insert divider success')
			}
		})
	},
	clear() {
		this.editorCtx.clear({
			success: function(res) {
				console.log("clear success")
			}
		})
	},
	removeFormat() {
		this.editorCtx.removeFormat()
	},
	insertDate() {
		const date = new Date()
		const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
		this.editorCtx.insertText({
			text: formatDate
		})
	},
	insertImage() {
		let self = this;
		uni.chooseImage({
			count: 1,
			success: (res) => {
				let localUrl = res.tempFilePaths[0];
				const uploadTask = uni.uploadFile({
					url: appconst.remoteServiceBaseUrl  + "/Picture/UploadPicture",
					filePath: localUrl,
					name: 'file',
					formData: {
						pictureType: 'Other'
					},
					success: function(res) {
						var data = res.data;
						console.warn('如发现没有获取到返回值请到源码191行修改后端返回图片路径以便正常使用插件', data);
						try {
							//Tip : 切记->主要修改这里图片的返回值为真实返回路径!!! 详情见示例
							data = JSON.parse(res.data).result;
						} catch (e) {
							throw (e, data);
						}
						// 根据自己的返回数据做相应判断,服务器返回200即代表成功请求
						if (res.statusCode == 200) {
							self.editorCtx.insertImage({
								src: appconst.remoteServiceBaseUrl + data.url,
								alt: '文章图片',
								success: function() {
									console.log('insert image success')
								}
							})
						} else {
							if (fail) {
								fail(data);
							}
						}
					},
					fail: function(res) {
						console.log(res);
						if (fail) {
							fail(res);
						}
					}
				});
			}
		})
	}
}